package com.bjut.dao.impl;

import java.util.List;

import com.bjut.dao.EmployeeDao;
import com.bjut.domain.Employee;

public class EmployeeDaoImpl extends HRglxtHibernateDaoSupport implements EmployeeDao{
 
	public void save(Employee employee)
	{  
	getHibernateTemplate().persist(employee);
	} 
	
	public Employee get(int id){
		return (Employee) getHibernateTemplate().get(Employee.class, id);
	}

	public void delete(Employee employee) {
		getHibernateTemplate().delete(employee);
	}

	public void delete(int id) {
		getHibernateTemplate().delete(get(id));
	}
    public Employee findById(int id){
       	return (Employee)getHibernateTemplate().get(Employee.class, id);
    }
    
    public Employee findByEmpId(int empid){
    	List result=getHibernateTemplate().find("from Employee as e where e.empid = ?" , 
				empid);
		if (result != null && result.size() > 0)
			return (Employee) result.get(0);
		else 
			return null;
    }
    
	public void update(Employee employee) {
		getHibernateTemplate().update(employee);
	}
	
	public Employee findEmployeeByname(String name) {
		List result=getHibernateTemplate().find("from Employee as e where e.name = ?" , 
				new String[]{name});
		if (result != null && result.size() > 0)
			return (Employee) result.get(0);
		else 
			return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<Employee> findAllByPage(int pageNo, int pageSize) {
		if (pageNo < 1)
		{
			return null;
		}
		int offset = (pageNo - 1) * pageSize;
		return findByPage("from Employee as e order by e.id", offset , pageSize);
	}

	public long getCount() {
		List employeeList = getHibernateTemplate().find("select count(e.id) from Employee as e");
		if (employeeList == null)
		{
			return 0;
		}
		return (Long)employeeList.get(0);
	}


	public List<Employee> findAll() {
		return getHibernateTemplate().find("from Employee as e order by e.id");
	}

	public List<Employee> findByDepartmentAndPage(int department_id,
			int pageNo, int pageSize) {
			if (pageNo < 1)
			{
				return null;
			}
			int offset = (pageNo - 1) * pageSize;
			return findByPage("from Employee as e where department_id=? order by e.id",department_id, offset , pageSize);
	}

	public long getCountByDepartment(int department_id) {
		List positionList = getHibernateTemplate().find("select count(e.id) from Employee as e where department_id=?",department_id);
		if (positionList == null)
		{
			return 0;
		}
		return (Long)positionList.get(0);
	}

}
